/**
 * 门诊交易接口：提示和审核
 */


/**
 * remind
 * 本接口在医师开具处方前调用，向医师提示病人的相关信息及提示风险。医院必须上传社保卡号码、科室、医师等关键信息，审核系统返回医院HIS提示信息
 */
var auth_token="gRmGkKS2nJqrxSa4XY8QF7PVuH1xmSZS2CDaFUwgjA9rG0Agl-FNexJbTL3TGZcSMs7mz-galUo5W3KuKnohTA";//智慧医疗软件的密钥
var ip="";
var mac="";
function remind(row)
{
	var tran_serial_no = hccode+new Date().Format("yyyyMMdd")+parseInt(100*Math.random());
	if (valueArr['p_9999_9999_103']==1 || valueArr['p_9999_9999_103']=='1')
	{
		//获取ip和mac
		$.ajax(
		{
		url : rootUrl + "/clidoctor/getIpAndMac",
		type : "get",
		dataType : "json",
		data : {},
		success : function(data)
		{
			debugger;
			if (data && data.result == "Success")
			{
				ip = data.map.ip;
				mac = data.map.mac;
			}
		}
		})
			//				var id = $("#myjiezhenTableList").jqGrid('getGridParam','selrow');
			//				var row = $("#myjiezhenTableList").jqGrid("getRowData",id);
			var sex = row ? row.abw02 : "";
			if(sex=="男")
			{
				sex=="1";
			}
			else if(sex=="女")
			{
				sex=="2";
			}
			else
			{
				sex=="3";
			}
			var params =
			{
			tran_serial_no : tran_serial_no,
			operate_ip : ip,
			operate_mac : mac,
			operate_machine_code : "",
			operate_person_code : loginName,
			operate_person_name : $.cookie("trueName"),
			operate_time : new Date().Format("yyyyMMdd/hhmmss"),
			visit_no : row.vaa03,
			patient_evidence_type : "02",
			card_no : row.vaa17,
			medical_dept_code : $('#departmentList').find("option:selected").attr("name"),
			medical_dept_name : $('#departmentList').find("option:selected").text(),
			visit_type : "2",
			doctor_code : loginName,
			doctor_name : $.cookie("trueName"),
			pc_no : row.vaa17,
			patient_name : row.vaa05,
			sex : sex,
			age : row ? row.vaa10 : "",
			birth_date : "",
			id_no : "",
			insurance_type : row.iax04,
			region_code : ""
			};
			console.log(JSON.stringify(params));
			$.ajax(
			{
			url : zhybUrl ? zhybUrl : "",
			type : "post",
			dataType : "json",
			data :
			{
			auth_token : auth_token,
			public_type : "remind",
			content : JSON.stringify(params)
			},
			success : function(data)
			{
				if (data)
				{
					if (data.success == "F")
					{
						console.log(data.error_msg);
						return false;
					}
					else if (data.success == "T" && data.window_url && data.window_url != "null")
					{
						validFlg = false;
						layer.open(
						{
						type : 2,
						scrollbar : true,
						title : "智慧医保提示",
						area : [
						"800px", "600px"
						],
						shade : 0.1,
						content : data.window_url,
						closeBtn : 1,
						success : function(){}
						})
					}
					else
					{
						console.log(data);
					}
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown)
			{
//				layer.msg("智慧医保服务连接失败，请联系管理员！",
//				{
//				time : 3000,
//				icon : 0,
//				skin : 'layui-layer-msg0'
//				});
			}
			});
	}

}



/**
 * audit
 * 本接口在医师开具处方前调用，向医师提示病人的相关信息及提示风险。医院必须上传社保卡号码、科室、医师等关键信息，审核系统返回医院HIS提示信息
 */
function audit(advice_details,vaf1,data1,isSend)
{
	debugger;
	var t1 = new Date().getTime();
	var tran_serial_no = hccode+new Date().Format("-yyyyMMddhhmmss-")+parseInt(100*Math.random());
	if (valueArr['p_9999_9999_103']==1 || valueArr['p_9999_9999_103']=='1')
	{
		if (advice_details && advice_details.length>0)
		{
				//获取ip和mac
				$.ajax(
				{
				url : rootUrl + "/clidoctor/getIpAndMac",
				type : "get",
				dataType : "json",
				async:false,
				data : {},
				success : function(data)
				{
					debugger;
					if (data && data.result == "Success")
					{
						ip = data.map.ip;
						mac = data.map.mac;
					}
				}
				})
			var id=$($(parent.document).find("#myjiezhenTableList")).jqGrid('getGridParam','selrow');
			var row = $($(parent.document).find("#myjiezhenTableList")).jqGrid('getRowData', id);
			var sex = row ? row.abw02 : "";
			if(sex=="男")
			{
				sex=="1";
			}
			else if(sex=="女")
			{
				sex=="2";
			}
			else
			{
				sex=="3";
			}
			var params =
			{
				tran_serial_no : tran_serial_no,
				operate_ip : ip,
				operate_mac : mac,
				operate_machine_code : "",
				operate_person_code : loginName,
				operate_person_name : $.cookie("trueName"),
				operate_time : new Date().Format("yyyyMMdd/hhmmss"),
				visit_no : row.vaa03,
				patient_evidence_type : "02",
				card_no : row.vaa17?row.vaa17:'',
				medical_dept_code :$($(parent.document).find('#departmentList')).find("option:selected").attr("name"),
				medical_dept_name : $($(parent.document).find('#departmentList')).find("option:selected").text(),
				visit_type : "2",
				medicine_type:"101",
				doctor_code : loginName,
				doctor_name : $.cookie("trueName"),
				pc_no : row.vaa17?row.vaa17:'',
				patient_name : row.vaa05,
				sex : sex,
				age : row.vaa10 ? row.vaa10 : "0",
				birth_date : "",
				id_no : "",
				insurance_type : row.iax04?row.iax04:'',
				region_code : ""
			};
			// diagnoses
			var vaoLength = $(".bootstrap-tagsinput").children("span").length;
			var diagnoses = [];
			for (var i = 0; i < vaoLength; i++)
			{
				var vao1t = {};
				var temp = $($(".bootstrap-tagsinput").children("span")[i]).text();
				var str = $($(".bootstrap-tagsinput").children("span")[i]).attr("data-info");
				var dobj = JSON.parse(str);
				if (dobj.cam01)
				{
					vao1t.cam01 = dobj.cam01;
					vao1t.diagnose_code = "0";// 疾病id-
				}
				else if (dobj.bak01)
				{
					vao1t.cam01 = "0";
					vao1t.diagnose_code = dobj.bak01;// 疾病id-
				}
				else if(dobj.iad03)
				{
					vao1t.cam01 = "0";
					vao1t.diagnose_code = dobj.iad03;// 疾病id-
				}
				if (str == "-999")// 如果为手动添加的疾病
				{
					vao1t.diagnose_code = cam1Map[temp];
				}
				vao1t.diagnose_desc = temp;
				vao1t.diagnose_no = i+1;
				diagnoses.push(vao1t);
			}
			params.advice_details = advice_details;
			params.diagnoses = diagnoses;
			var t2 = new Date().getTime();
			console.log("------------智慧医保组装数据耗时："+(t2-t1)+"毫秒---------------");
			$.ajax({
				url:zhybUrl ? zhybUrl : "",
				data:
				{
				auth_token : auth_token,
				public_type : "audit",
				content : JSON.stringify(params)
				},
				type:"post",
				dataType:"json",
				timeout:500,
				success:function(data)
				{
					var t3 = new Date().getTime();
					console.log("------------智慧医保远程请求耗时："+(t3-t2)+"毫秒---------------");
						if (data.success == "T" && data.window_url && data.window_url != "null")
						{
//							validFlg = false;
							layer.open(
							{
							type : 2,
							scrollbar : true,
							title : "智慧医保提示",
							area : [
							"800px", "600px"
							],
							shade : 0.1,
							content : data.window_url,
							closeBtn : 1,
							success : function(){
								 layer.closeAll('dialog');
							},
							cancel: function(){ 
								debugger;
								layer.confirm('继续发送医嘱还是返回修改医嘱？',
										{
											btn : [
											'继续发送', '返回修改'
											]
										// 按钮
										}, function()
										{
											afterAudit_dange(vaf1,data1,isSend);// 继续发送医嘱
										}, function()
										{
											layer.closeAll('dialog'); //关闭信息框，返回修改医嘱
											$(thisbutton).removeAttr("disabled");
										});
								}
							})
						}
						else
						{
							console.log(data);
							afterAudit(vaf1,data1,isSend);
						}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown)
				{
					var t3 = new Date().getTime();
					console.log("------------智慧医保远程请求耗时："+(t3-t2)+"毫秒---------------");
					afterAudit(vaf1,data1,isSend);
//					layer.msg("智慧医保服务连接失败，请联系管理员！",
//					{
//					time : 3000,
//					icon : 0,
//					skin : 'layui-layer-msg0'
//					});
				}
				});
		}
		else
		{
			afterAudit(vaf1,data1,isSend);
		}
		}
	else
	{
		afterAudit(vaf1,data1,isSend);
	}
}
/**
 * audit
 * 本接口在医师开具处方前调用，向医师提示病人的相关信息及提示风险。医院必须上传社保卡号码、科室、医师等关键信息，审核系统返回医院HIS提示信息
 */
function audit_dange(advice_details,vaf1,data1,isSend)
{
	debugger;
	var t1 = new Date().getTime();
	var tran_serial_no = hccode+new Date().Format("-yyyyMMddhhmmss-")+parseInt(100*Math.random());
	if (valueArr['p_9999_9999_103']==1 || valueArr['p_9999_9999_103']=='1')
	{
		if (advice_details && advice_details.length>0)
		{
			//获取ip和mac
			$.ajax(
					{
						url : rootUrl + "/clidoctor/getIpAndMac",
						type : "get",
						dataType : "json",
						async:false,
						data : {},
						success : function(data)
						{
							debugger;
							if (data && data.result == "Success")
							{
								ip = data.map.ip;
								mac = data.map.mac;
							}
						}
					})
					var id=$($(parent.document).find("#myjiezhenTableList")).jqGrid('getGridParam','selrow');
			var row = $($(parent.document).find("#myjiezhenTableList")).jqGrid('getRowData', id);
			var sex = row ? row.abw02 : "";
			if(sex=="男")
			{
				sex=="1";
			}
			else if(sex=="女")
			{
				sex=="2";
			}
			else
			{
				sex=="3";
			}
			var params =
			{
					tran_serial_no : tran_serial_no,
					operate_ip : ip,
					operate_mac : mac,
					operate_machine_code : "",
					operate_person_code : loginName,
					operate_person_name : $.cookie("trueName"),
					operate_time : new Date().Format("yyyyMMdd/hhmmss"),
					visit_no : row.vaa03,
					patient_evidence_type : "02",
					card_no : row.vaa17?row.vaa17:'',
							medical_dept_code :$($(parent.document).find('#departmentList')).find("option:selected").attr("name"),
							medical_dept_name : $($(parent.document).find('#departmentList')).find("option:selected").text(),
							visit_type : "2",
							medicine_type:"101",
							doctor_code : loginName,
							doctor_name : $.cookie("trueName"),
							pc_no : row.vaa17?row.vaa17:'',
									patient_name : row.vaa05,
									sex : sex,
									age : row.vaa10 ? row.vaa10 : "0",
											birth_date : "",
											id_no : "",
											insurance_type : row.iax04?row.iax04:'',
													region_code : ""
			};
			// diagnoses
			var vaoLength = $(".bootstrap-tagsinput").children("span").length;
			var diagnoses = [];
			for (var i = 0; i < vaoLength; i++)
			{
				var vao1t = {};
				var temp = $($(".bootstrap-tagsinput").children("span")[i]).text();
				var str = $($(".bootstrap-tagsinput").children("span")[i]).attr("data-info");
				var dobj = JSON.parse(str);
				if (dobj.cam01)
				{
					vao1t.cam01 = dobj.cam01;
					vao1t.diagnose_code = "0";// 疾病id-
				}
				else if (dobj.bak01)
				{
					vao1t.cam01 = "0";
					vao1t.diagnose_code = dobj.bak01;// 疾病id-
				}
				if (str == "-999")// 如果为手动添加的疾病
				{
					vao1t.diagnose_code = cam1Map[temp];
				}
				vao1t.diagnose_desc = temp;
				vao1t.diagnose_no = i+1;
				diagnoses.push(vao1t);
			}
			params.advice_details = advice_details;
			params.diagnoses = diagnoses;
			var t2 = new Date().getTime();
			console.log("------------智慧医保组装数据耗时："+(t2-t1)+"毫秒---------------");
			$.ajax({
				url:zhybUrl ? zhybUrl : "",
						data:
						{
							auth_token : auth_token,
							public_type : "audit",
							content : JSON.stringify(params)
						},
						type:"post",
						dataType:"json",
						timeout:500,
						success:function(data)
						{
							var t3 = new Date().getTime();
							console.log("------------智慧医保远程请求耗时："+(t3-t2)+"毫秒---------------");
							if (data.success == "T" && data.window_url && data.window_url != "null")
							{
//								validFlg = false;
								layer.open(
										{
											type : 2,
											scrollbar : true,
											title : "智慧医保提示",
											area : [
											        "800px", "600px"
											        ],
											        shade : 0.1,
											        content : data.window_url,
											        closeBtn : 1,
											        success : function(){
											        	layer.closeAll('dialog');
											        },
													cancel: function(){ 
														
														layer.confirm('继续发送医嘱还是返回修改医嘱？',
																{
																	btn : [
																	'继续发送', '返回修改'
																	]
																// 按钮
																}, function()
																{
																	afterAudit_dange(vaf1,data1,isSend);// 继续发送医嘱
																}, function()
																{
																	layer.closeAll('dialog'); //关闭信息框，返回修改医嘱
																	$(thisbutton).removeAttr("disabled");
																});
														}
										})
							}
							else
							{
								console.log(data);
								afterAudit_dange(vaf1,data1,isSend);
							}
						},
						error : function(XMLHttpRequest, textStatus, errorThrown)
						{
							var t3 = new Date().getTime();
							console.log("------------智慧医保远程请求耗时："+(t3-t2)+"毫秒---------------");
							afterAudit_dange(vaf1,data1,isSend);
//					layer.msg("智慧医保服务连接失败，请联系管理员！",
//					{
//					time : 3000,
//					icon : 0,
//					skin : 'layui-layer-msg0'
//					});
						}
			});
		}
		else
		{
			afterAudit_dange(vaf1,data1,isSend);
		}
	}
	else
	{
		afterAudit_dange(vaf1,data1,isSend);
	}
}

function RndNum(n)
{
	  var rnd="";
	  for(var i=0;i<n;i++)
	  {
	     rnd+=Math.floor(Math.random()*10);
	  }
	  return rnd;
	}

